package cn.maigc.web.sys_menu.controller;

import cn.maigc.utils.ResultVo;
import cn.maigc.web.sys_menu.entity.MakeMenuTree;
import cn.maigc.web.sys_menu.entity.SysMenu;
import cn.maigc.web.sys_menu.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name = "控制器：测试Swagger3", description = "描述：测试Swagger3")
@RestController
@RequestMapping("/api/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    //增加
    @PostMapping
    public ResultVo add(@RequestBody SysMenu sysMenu){
        sysMenu.setCreateTime(new Date());
        boolean temp = sysMenuService.save(sysMenu);
        if(temp){
            return ResultVo.success("新增成功！！");
        }
        return ResultVo.error("新增失败！！");
    }

    //删除
    @DeleteMapping("/{menuId}")
    public ResultVo delete(@PathVariable("menuId") long menuId){
        boolean temp = sysMenuService.removeById(menuId);
        if(temp){
            return ResultVo.success("删除成功！！");
        }
        return ResultVo.error("删除失败！！");
    }
    //修改
    @PutMapping
    public ResultVo edit(@RequestBody SysMenu sysMenu){
        sysMenu.setUpdateTime(new Date());
        boolean temp = sysMenuService.updateById(sysMenu);
        if(temp){
            return ResultVo.success("修改成功！！");
        }
        return ResultVo.error("修改失败！！");
    }

    //查询
    @GetMapping("/list")
    public ResultVo list(){

        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        //排序
        wrapper.lambda().orderByAsc(SysMenu::getOrderNum);
        //查询所有的菜单
        List<SysMenu> list = sysMenuService.list(wrapper);
        //组装成树
        List<SysMenu> menuList = MakeMenuTree.makeTree(list,0L);

        if(menuList.size() > 0){
            return ResultVo.success("查询成功！！",menuList);
        }
        return ResultVo.error("查询失败！！");
    }

    //上级菜单
    @GetMapping("/getParent")
    public ResultVo getParent(){
        List<SysMenu> list = sysMenuService.getParent();
        return ResultVo.success("查询成功！！",list);
    }

}
